{% extends 'app01/base_backend.html' %}
{% block person_manage %}
    <a class="navbar-brand" href="#">上传web资源</a>
{% endblock %}
{% block all_li %}
    <li role="presentation">
        <a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">上传web资源</a>
    </li>
    <li role="presentation" class="active">
        <a href="{% url 'app01_backend' %}" style="color: #337ab7">返回个人管理</a></li>
{% endblock %}

{% block all_content %}
    {% block caogao %}
        <div class="panel panel-default">
            <div class="panel-heading">资源上传</div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-8 col-md-offset-2">

                        资源名：
                        <input type="text" class="form-control" id="source_name_id" name="sou_name">
                        资源描述：
                        <input type="text" class="form-control" id="source_desc_id" name="sou_desc">
                        资源分类：
                        <br>
                        {% for ca in cate %}
                            {{ ca.name }}<input type="radio" class="web_choice_class"   name="web_radio" value="{{ ca.pk }}">
                            {% empty %}
                             <a class="btn btn-block btn-success" href="/app01/web_sourse/">点击创建资源分类</a>
                             <input id="for_kown_empty" value="false" hidden >
{#                            判断如果该id存在，且text是emoty，则不能发起发布资源请求，在后端要拒绝没有分类的请求#}
                        {% endfor %}
                        <br>
                        <br>


                        运行效果图：
                        <input type="file" class="form-control" id="source_img_id" name="sou_img">
                        <br>
                        代码文件：
                        <input type="file" class="form-control" id="source_code_id" name="sou_code">
                        <br>
                        <button class="btn btn-primary btn-block" id="submit_up_id">提交</button>

                    </div>
                </div>
            </div>
        </div>




    {% endblock %}

{% endblock %}
{% block js %}
    <script>
        if ('{{ request.user.username }}'.slice(0, 3) === '512') {
            $('#p_for_a').html('<a href="/app01/manage_student/" class="list-group-item">学生管理</a>')
            $('#role_for').html('角色：<br><a href="#">老师</a>')
        }


        if ('{{ request.user.username }}'.slice(0, 3) === '312') {

            $('#set_person_id').html('<p>班级：<input type="text" id="stu_class" class="form-control" value="{{ request.user.stu_class }}"></p>')
            $('#role_for').html('角色：<br><a href="#">学生</a>')
        }


        //ajax实现web资源的上传
    $('#submit_up_id').click(function () {

        let sou_name = $('#source_name_id').val()
        let sou_desc = $('#source_desc_id').val()
        let sou_img = $('#source_img_id')[0].files[0]
        let sou_code = $('#source_code_id')[0].files[0]
        var cate_id = $("input[name='web_radio']:checked").val()
        let formdata = new FormData()
        formdata.append('sou_name',sou_name)
        formdata.append('sou_desc',sou_desc)
        formdata.append('sou_img',sou_img)
         formdata.append('sou_code',sou_code)
        formdata.append('cate_id',cate_id)
        let empty = $('#for_kown_empty').val()
        if (empty==='false'){
            alert('需要选择分类，请先创建分类。')
        }
        else {
              $.ajax({
            url:'',
            type:'post',
            data:formdata,
            contentType:false,
            processData:false,
            success:function (attr) {
                if(attr.code===100){
                    alert(attr.msg)
                    window.location.reload()
                }
                else {
                    alert(attr.msg)
                }

            }
        })
        }
    })

     //文件上传的ajax请求
        $("#up_btn_id").click(function () {
            let formObj = new FormData()
            formObj.append('file', $('#up_file_id')[0].files[0])
            formObj.append('filename', $('#file_name_id').val())
            formObj.append('desc', $('#file_desc_id').val())
            let radio_chioce = $('input[name="share"]:checked').val();
            formObj.append('share', radio_chioce)

            $.ajax({
                url: "/app01/upload_file/",
                type: 'post',
                data: formObj,
                contentType: false,
                processData: false,
                success: function (array) {
                    if (array.code === 1000) {
                        alert(array.msg0)
                        parent.location.href = array.url
                    } else if (array.code === 2000) {
                        //用户输入不正确的提示
                        $('#p1').next().text(array.msg1)
                        $('#p2').next().text(array.msg2)
                        $('#p3').next().text(array.msg3)
                    } else {
                        alert(array.msg0)
                    }

                }
            })


        });
    </script>
{% endblock %}